<template>
  <div id="job" class="container">
    <h3 class="title"><span />岗位管理</h3>
    <el-button
      type="primary"
      class="add four"
      icon="el-icon-plus"
      size="small"
      style="margin-bottom:15px"
      @click="add"
    >添加岗位</el-button>
    <el-table
      :data="jobList"
      border
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
    >
      <!-- <el-table-column label="ID" width="80px" prop="id" /> -->
      <el-table-column label="岗位名" prop="title" />
      <el-table-column label="工作内容" prop="duty" show-overflow-tooltip />
      <el-table-column label="工作要求" prop="demand" show-overflow-tooltip />
      <el-table-column label="工作要求" prop="description" show-overflow-tooltip />
      <el-table-column label="工资" prop="salary" width="120px" />
      <el-table-column label="收件箱" prop="email" />
      <el-table-column label="发布日期" prop="schedule" />
      <el-table-column label="操作" width="250px" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" class="edit" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" class="dele" @click="del(scope.row)">删除</el-button>
          <!-- <el-button size="mini" @click="check(scope.row)">查看</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <BouncedDialog :title="addForm.id?'编辑':'添加'" :dialog-show.sync="dialogShow" :width="50" class="inner">
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        inline
        class="addForm"
        label-width="100px"
        size="small"
      >
        <el-form-item label="发布站点:" prop="website_id">
          <el-select v-model="addForm.website_id">
            <el-option
              v-for="item in website"
              :key="item.id"
              :label="item.domain"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="岗位名称:" prop="title">
          <el-input v-model="addForm.title" placeholder="请输入岗位名" />
        </el-form-item>
        <el-form-item label="职位要求:" prop="demand">
          <el-input v-model="addForm.demand" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入工作要求" />
        </el-form-item>
        <el-form-item label="工资:" prop="salary">
          <el-input v-model="addForm.salary" placeholder="请输入岗位工资" />
        </el-form-item>
        <el-form-item label="岗位分类:">
          <el-select v-model="addForm.job_cate_id">
            <el-option
              v-for="item in cate"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作城市:">
          <el-select v-model="addForm.job_city_id">
            <el-option
              v-for="item in city"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布日期">
          <el-date-picker
            v-model="addForm.schedule"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="收件箱:" prop="email">
          <el-input v-model="addForm.email" placeholder="请输入收件箱" />
        </el-form-item>

        <el-form-item label="工作内容:" prop="duty">
          <el-input v-model="addForm.duty" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入工作内容" />
        </el-form-item>
        <el-form-item label="岗位描述:">
          <el-input v-model="addForm.description" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入岗位描述" />
        </el-form-item>
        <div class="btn">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-form>
    </BouncedDialog>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total<page.size?true:false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import jobApi from '@/api/job.js'
import websiteApi from '@/api/website'
export default {
  data() {
    return {
      jobList: [],
      addForm: {
        title: '',
        job_cate_id: '',
        job_city_id: '',
        description: '',
        duty: '',
        demand: '',
        schedule: '',
        email: '',
        salary: '',
        website_id: '',
        id: ''
      },
      rules: {
        website_id: [
          { required: 'true', message: '请选择站点', trigger: 'change' }
        ],
        title: [
          { required: 'true', message: '请输出入岗位名称', trigger: 'blur' }
        ],
        demand: [
          { required: 'true', message: '请输出入职位要求', trigger: 'blur' }
        ]
      },
      dialogShow: false,
      city: [], // 城市
      cate: [], // 岗位分类
      website: [], // 站点
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      siteId: ''
    }
  },
  computed: {
    siteId1() {
      return this.$store.getters.siteId
    }
  },
  watch: {
    siteId1(val, value) {
      this.siteId = val
      this.addForm.website_id = val
    }
  },
  created() {
    // this.addForm.website_id = this.$store.getters.siteId
    this.getList()
    this.getCity()
    this.getJobCate()
    this.getWebsite()
  },
  methods: {
    // 站点列表
    getWebsite() {
      websiteApi.websiteList().then(res => {
        this.website = res.data
      })
    },
    // 城市列表
    getCity() {
      jobApi.jobCityList().then(res => {
        this.city = res.data
      })
    },
    // 岗位分类列表
    getJobCate() {
      jobApi.jobCateList().then(res => {
        this.cate = res.data
      })
    },
    //   岗位列表
    getList() {
      const val = {
        page: this.page.currentPage,
        limit: this.page.size
      }
      jobApi.jobList(val).then(res => {
        this.jobList = res.data
        // this.jobList.reverse()
        this.page.total = res.count
      })
    },
    // 添加
    add() {
      this.addForm.id = ''
      this.addForm = {
        title: '',
        job_cate_id: '',
        job_city_id: '',
        description: '',
        duty: '',
        demand: '',
        schedule: '',
        email: '',
        salary: '',
        website_id: this.$store.getters.siteId
      }
      this.dialogShow = true
    },
    // 编辑
    edit(row) {
      this.addForm.id = row.id
      this.getJobInfo(row)
      this.dialogShow = true
    },
    // 岗位详情
    getJobInfo(val) {
      jobApi.jobInfo({ id: val.id, website_id: val.website_id }).then(res => {
        this.addForm = res.data
      })
    },
    // 提交
    submit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (!this.addForm.id) {
            jobApi.jobSave({ ...this.addForm }).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            const val = {
              title: this.addForm.title,
              job_cate_id: this.addForm.job_cate_id,
              job_city_id: this.addForm.job_city_id,
              description: this.addForm.description,
              duty: this.addForm.duty,
              demand: this.addForm.demand,
              schedule: this.addForm.schedule,
              email: this.addForm.email,
              salary: this.addForm.salary,
              website_id: this.addForm.website_id,
              id: this.addForm.id
            }
            jobApi.jobUpdate(val).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该岗位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        jobApi.jobDelete({ id: row.id }).then(res => {
          if (res.code == 0) {
            this.$message.success('已删除')
            this.getList()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    check() {},
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }
  }
}
</script>
<style lang='scss'>
#job{
  .addForm{
    .el-form-item{
      width: 100%;
    }
    .el-form-item__content{
      width: calc(100% - 100px);
      .el-select, .el-date-editor{
        width: calc(100%);
      }
    }
  }
}
</style>
<style scoped lang='scss'>

</style>
